# 等额本息 h换算方式
# 输入参数:贷款总额，贷款利率，贷款期数
def calculate_loane_equal_principal_and_interest(principal, interest_rate, num_periods):
    # 将年利率转换为月利率
    monthly_interest_rate = interest_rate / 12
    # 将计算公式转换为Python程序表达式
    # (1+R)** N
    snum = num_periods * 12
    principal = principal * 10000
    RN = (1 + monthly_interest_rate) ** snum
    # 每月还款额
    monthly_payment = principal * (monthly_interest_rate * RN / (RN - 1))
    # 共计还款额
    total_payment = monthly_payment * snum
    # 总利息
    interest_paid = total_payment - principal
    return monthly_payment, total_payment, interest_paid


# 等额本金
def calculate_loane_equal_principal_amount(principal, annual_interest_rate, years):
    # 根据年利率计算月利率
    monthly_interest_rate = annual_interest_rate / 12
    # 还款期数
    total_months = years * 12
    principal = principal * 10000
    # 每月固定的还款本金
    monthly_payment = principal / total_months
    # 剩余本金
    remaining_principal = principal
    # 总利息
    total_interest = 0
    jsonlsit = []
    lss = []
    for month in range(1, total_months + 1):
        # 当月利息
        interest = remaining_principal * monthly_interest_rate
        # 累汁汁算怠利息
        total_interest += interest
        # 下个月的还款本金(贷款本金逐月减少)
        remaining_principal -= monthly_payment
        # month :期数
        # monthly_payment 当期归还本金
        # interest 当期归还利息
        # monthly_payment_interest  当期归还总额
        # remaining_principal 剩余本金

        ls = {"month": month, "monthly_payment": format(monthly_payment, ".2f"),
              "interest": format(interest, ".2f"),
              "monthly_payment_interest": format(monthly_payment + interest, ".2f"),
              "remaining_principal": format(remaining_principal, ".2f")}
        lss.append(ls)
    jsonlsit.append(lss)
    jsonlsit.append(format(principal + total_interest, ".2f"))
    jsonlsit.append(format(total_interest, ".2f"))

    #     返回数据：每期的还款数据 总还款额  总利息
    return lss, format(principal + total_interest, ".2f"), format(total_interest, ".2f")
